package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.constant.Constant;
import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.mapper.PropertyMapper;
import cn.wolfcode.shop.service.ICatalogService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

@Service
public class CatalogServiceImpl implements ICatalogService {
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PropertyMapper propertyMapper;


    @Override
    public List<Catalog> selectAll() {
        List<Catalog> catalogsCache = (List<Catalog>) redisTemplate.opsForValue().get(Constant.CATALOG_REDIS);
        if(catalogsCache ==null){
            catalogsCache = getRedisCatalogsCache();
        }
        return catalogsCache;
    }

    /**
     * Redis缓存
     * @return
     */
    private List<Catalog> getRedisCatalogsCache() {
        List<Catalog> catalogsCache= catalogMapper.selectAll();
        redisTemplate.opsForValue().set(Constant.CATALOG_REDIS, catalogsCache);
        return catalogsCache;
    }

    /**
     * 商品列表(根据商品的pid查询)
     *
     * @param catalogId
     * @return
     */
    @Override
    public List<Catalog> selectByCatalogPid(Long catalogId) {

        List<Catalog> catalogsCache = catalogMapper.selectByCatalogPid(catalogId);
        //定义当前商品属性的个数
        Integer catalogPropertyCount;
        //遍历该商品列表
        for (Catalog catalog : catalogsCache) {
            //定义查询缓存的key "catalog:{0}:property:count"
            String catalogPropertyCountKey = MessageFormat.format(Constant.CATALOG_PROPERTY_COUNT, catalog.getId());
            //根据key 获取redis中的缓存
            catalogPropertyCount = (Integer) redisTemplate.opsForValue().get(catalogPropertyCountKey);
            //判断是否有缓存
            if(catalogPropertyCount ==null){
                //查询商品的属性个数
                catalogPropertyCount=propertyMapper.catalogPropertyCountByCatalogId(catalog.getId());
                //放入缓存中
                redisTemplate.opsForValue().set(catalogPropertyCountKey,catalogPropertyCount);

            }
            catalog.setPropertyCount(catalogPropertyCount);
        }
        return catalogsCache;
    }

    /**
     * 商品添加分类
     *
     * @param catalog
     */
    @Override
    public void saveOrUpdate(Catalog catalog) {

        if (catalog.getId() != null) {
            catalogMapper.updateByPrimaryKey(catalog);
        } else {
            //查询上级是否是目录,如果不是将其改成目录
            Catalog parentCatalog = catalogMapper.selectByPrimaryKey(catalog.getPId());
            if (!parentCatalog.isParent()) {
                parentCatalog.setParent(true);
                catalogMapper.updateByPrimaryKey(parentCatalog);
            }
            catalogMapper.insert(catalog);

        }
        getRedisCatalogsCache();
    }

    /**
     * 查询该分类是否是目录,如果是不能删除
     *
     * @param catalogId
     * @return
     */
    @Override
    public Catalog selectByCatalogId(Long catalogId) {
        return catalogMapper.selectByPrimaryKey(catalogId);
    }

    /**
     * 查询当前分类下有几个字分类(用于删除判断)
     *
     * @param catalogId
     * @return
     */
    @Override
    public List<Catalog> selectChildByCatalog(Long catalogId) {
        Catalog catalog = catalogMapper.selectByPrimaryKey(catalogId);
        return catalogMapper.selectByCatalogPid(catalog.getPId());
    }

    /**
     * 删除当前分类
     * @param catalogId
     */
    @Override
    public void delete(Long catalogId) {
        catalogMapper.deleteByPrimaryKey(catalogId);
        getRedisCatalogsCache();
    }

    /**
     * 给需要做拖拽排序
     * @param ids
     */
    @Override
    public void updateSort(Long[] ids) {
        for (int i = 0; i <ids.length ; i++) {
            Long id = ids[i];
            Catalog catalog = catalogMapper.selectByPrimaryKey(id);
            catalog.setSort(i+1);
            catalogMapper.updateByPrimaryKey(catalog);
        }
        getRedisCatalogsCache();
    }

    /**
     * 查询更新属性个个数
     */
    @Override
    public void catalogPropertyCountByCatalog() {

//        DataSourceContext.setDsName(DataSourceContext.SLAVE_DS_NAME);

        //定义当前商品属性的个数
        Integer catalogPropertyCount;
        //后去商品属性表中的  不同商品的属性个数
        List<Map<String,Long>>  list=propertyMapper.catalogPropertyCountAll();

        for (Map<String, Long> map : list) {

            //Set<Map.Entry<Long, Integer>> entries = longIntegerMap.entrySet();

            //for (Map.Entry<Long, Integer> entry : entries) {
                String catalogPropertyCountKey = MessageFormat.format(Constant.CATALOG_PROPERTY_COUNT, map.get("catalogId"));

                //查询商品的属性个数
                //catalogPropertyCount=propertyMapper.catalogPropertyCountByCatalogId(entry.getKey());
                Catalog catalog = catalogMapper.selectByPrimaryKey(map.get("catalogId"));
                catalog.setPropertyCount( map.get("count").intValue());
                //catalogMapper.updateByPrimaryKey(catalog);
                //放入缓存中
                redisTemplate.opsForValue().set(catalogPropertyCountKey,map.get("count").intValue());
           // }
        }

    }
}
